package Thread;

public class ThreadDemo30 {
    public static void main(String[] args) {
        Object lockerA = new Object();
        Object lockerB = new Object();
        Object lockerC = new Object();

        Thread a = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.print("A");

                // 打印完A就要去唤醒B去打印
                synchronized (lockerB) {
                    lockerB.notify();
                }

                // 然后A自己进入WAITING状态，等待C来唤醒
                synchronized (lockerA) {
                    try {
                        lockerA.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread b = new Thread(() -> {
            // 确保a线程先执行
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < 10; i++) {
                System.out.print("B");

                // 打印完B要去唤醒C打印
                synchronized (lockerC) {
                    lockerC.notify();
                }

                // B进入等待状态，等待A来唤醒
                synchronized (lockerB) {
                    try {
                        lockerB.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        });

        Thread c = new Thread(() -> {
            // 确保ab线程先执行
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < 10; i++) {
                System.out.println("C");
                // 打印完C 就要去唤醒A继续打印
                synchronized (lockerA) {
                    lockerA.notify();
                }

                // 然后进入等待状态，等待B来唤醒
                synchronized (lockerC) {
                    try {
                        lockerC.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        });


        a.start();
        b.start();
        c.start();

    }
}
